const {
    onPageNotFound
} = require("../utils/err_util")
const controller = require("../controllers/goods")


async function handleGoodsRequest(req, res) {
    // res.end("[商品模块]响应请求")
    const rePath = /^\/goods\/(.*)$/
    const path = rePath.exec(req.url)[1]
    reGid = /^\/goods\/([\da-z]{24})$/
    rePage = /^\/goods\/list\/(\d+)$/


    switch (true) {
        //添加商品
        case path === "0" && req.method === "POST":
            res.end("添加商品成功")
            break;
            //删除商品
        case reGid.test(req.url) && req.method === "DELETE":
            res.end("删除商品成功")
            break;
            //修改商品
        case reGid.test(req.url) && req.method === "PUT":
            res.end("修改商品成功")
            break;
            //查询全部商品
        case path === "list/0" && req.method === "GET":
            // controller.getAllGoods().then(data => res.end(JSON.stringify(data)))
            //     .catch(
            //         ree => res.end(JSON.stringify(err))
            //     )
            try{ const data =await controller.getAllGoods()
                res.end(JSON.stringify(data))
            }catch{
                res.end(JSON.stringify(err))
            }
           
            break;
            //查询分页商品
        case rePage.test(req.url) && req.method === "GET":
            res.end(`获取第${rePage.exec(req.url)[1]}页商品`)
            break;

        default:
            onPageNotFound(req, res)
            break;
    }
}
module.exports =
    handleGoodsRequest